<?php
/**
 * 首页 ... 真心写得乱呀...
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Plan");
apf_require_class("Bll_Jx_Setting");
apf_require_class("Bll_Jx_Relation");
class HomeController extends AbstractController {
    public function handle_request() {
        $this->user_id = $this->request->get_user_id();
        $this->plan_bll = new Bll_Jx_Plan();
        $this->setting_bll = new Bll_Jx_Setting();
        $this->team_bll = new Bll_Jx_Relation();
        $this->view_user_info = $this->plan_bll->get_user_info_from_kq($this->user_id);

        $last_month = $this->setting_bll->format_performance();
        $curr_month = substr($last_month[1], 0,6);

        //考评月
        $review_days = $this->review_days_list($curr_month);
        //下属
        $sub_list = $this->sub_list();
        $team_list = array();
        if(!empty($sub_list)){
            $team_list = $this->build_lower_data($sub_list,$last_month);
        }
        $no_kp = $this->team_bll->get_kp_user(1);

        $plan_status = Const_JxStatus::plan_status();
        //我的考评动态
        $my_plan_list = $this->build_lower_data(array(array('user_id'=>$this->request->get_user_id())),$last_month);

        //取所有考评
        $my_score_list = $this->_get_my_score();

        $this->request->set_attribute('my_score_list',$my_score_list);
        $this->request->set_attribute('my_user_id',$this->request->get_user_id());
        $this->request->set_attribute('my_plan_list',$my_plan_list);
        $this->request->set_attribute('plan_status',$plan_status);
        $this->request->set_attribute('last_month',$last_month);
        $this->request->set_attribute('sub_list',$sub_list);
        $this->request->set_attribute('no_kp',$no_kp);
        $this->request->set_attribute('team_list',$team_list);
        $this->request->set_attribute('review_days', $review_days);
        $this->request->set_attribute('view_user_info', (array)$this->view_user_info);
        return 'Home';
    }
    /**
     * 我的历史得分,为空说明新入职还没有，考评或计划
     * @return multitype:
     */
    private function _get_my_score(){
        $res = array();
        $my_score_list = $this->plan_bll->get_user_plan_score($this->request->get_user_id());
        if(empty($my_score_list)){
            return $res;
        }
        $has_month = array();
        //ALTER TABLE  `user_performance` ADD  `is_exemption` TINYINT( 1 ) NOT NULL DEFAULT  '0' COMMENT  '1是免考默认为0' AFTER  `userId`
        //这里需要区分哪些人是免考的,以前是打2分当免考,
        foreach($my_score_list['datalist'] as $val){
            $has_month[] = $val['serachMonth'];
            if(!$val['is_exemption']){
                $res[$val['year']]['all']['score'] += $val['adjustScore'];
                $res[$val['year']]['all']['cnt'] += 1;
                $res[$val['year']]['list'][$val['serachMonth']] += $val['adjustScore'];
            }
        }
        //补数据,那些月份是没有的
        foreach($my_score_list['month_list'] as $day){
            if(!in_array($day,$has_month)){
                $year = substr($day,0,4);
                $res[$year]['list'][$day] = -1;
            }
        }
        //ksort($res);
        //汇总数据,根据先后 排序
        foreach($res as $year=>$val){
             ksort($val['list']);
             $res[$year]['list'] = $val['list'];
        }
        return $res;
    }
    /**
     * 计算应该是当前阶段，还是未操作阶段
     * @param int $curr_month 201304
     */
    private function review_days_list($curr_month){
        $review_days = $this->setting_bll->get_performance_days($curr_month);
        $current =0;
        $today = date('Ymd');
        foreach($review_days as $k=>$val){
            $review_days[$k]['class'] = 'no';
            if(!$current && $today <=$val['step_date']){
                $review_days[$k]['class'] = 'curr';
            }
            if($today >$val['step_date']){
                $review_days[$k]['class'] = 'pass';
            }
        }
        return $review_days;
    }

    private function sub_list(){
        $uid = $this->request->get_user_id();
        return $this->team_bll->get_lower_user($uid);
    }
    private function build_lower_data($sub_list,$last_month){

        $uids = array();
        foreach ($sub_list as $value) {
            $uids[] = $value['user_id'];
        }
        $_where = array(
                "userId IN(".implode(',',$uids).") AND 1= ?"=>1
        );
        $m_list = array();
        foreach ($last_month as $val){
            if(!empty($val['m_list'])){
                $m_list[] = $val;
            }
        }
        if(empty($m_list)){
            return array();
        }
        $_where["serachMonth IN(".implode(',',$m_list).") AND 1= ?"]=1;

        $data = $this->plan_bll->find_performance($_where);
        //按月归类
        $res = array();
        foreach($data as $val){
            $res[$val['userId']][$val['serachMonth']] = $val;
        }
        return $res;
    }
}